﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;

namespace MiniKsiegowosc.RaportTools
{
    public partial class FormRaportPrintSetup : XtraForm
    {
        protected FormRaportPrintSetup()
        {
            InitializeComponent();
        }

        public ReportPrintType PrintType
        {
            get
            {
                string str = radioGroup1.EditValue.AsString();
                switch (str)
                {
                    case "P":
                        return ReportPrintType.Preview;
                    case "E":
                        return ReportPrintType.ExportToPDF;
                    default :
                        return ReportPrintType.Print;
                }
            }
        }
        public string PdfFile
        {
            get { return fileDialogEdit1.EditValue.AsString(); }
        }
        public string Printer
        {
            get
            {
                return printerCombo.EditValue.AsString();
            }
        }

        public static DialogResult ShowDialog(XtraReport report)
        {
            return ShowDialog(new XtraReport[] { report });
        }

        public static DialogResult ShowDialog(XtraReport[] reports)
        {
            FormRaportPrintSetup f = new FormRaportPrintSetup();
            DialogResult d = f.ShowDialog();
            if (d == DialogResult.OK)
            {
                XtraReport r = new XtraReport();
                r.Pages.Clear();
                foreach (XtraReport report in reports)
                {
                    if (report is RaportTools.IDataReport)
                        (report as RaportTools.IDataReport).GetData();

                    report.CreateDocument();
                    r.Pages.AddRange(report.Pages);
                }

                switch (f.PrintType)
                {
                    case ReportPrintType.ExportToPDF:
                        r.ExportToPdf(f.PdfFile);
                        break;
                    case ReportPrintType.Preview:
                        r.ShowPreviewDialog();
                        break;
                    default:
                        r.Print(f.Printer);
                        break;
                }

                r.Dispose();
            }
            
            f.Dispose();
            return d;
        }

        private void radioGroup1_EditValueChanged(object sender, EventArgs e)
        {
            panelDrukarka.Visible = PrintType == ReportPrintType.Print;
            panelPdf.Visible = PrintType == ReportPrintType.ExportToPDF;
        }

    }
    
    public enum ReportPrintType
    {
        Print,
        ExportToPDF,
        Preview
    }
}
